/**
 * @file 记录用户的网站访问记录
 * 放到网站公共尾部，必须有jquery.js
 */

var ckPageUser = 0;
if (getCkCookie('_ck_uName')) {
    ckPageUser = getCkCookie('_ck_uName');
    setCkCookie('_ck_uName', ckPageUser);
} else {
    var ckPageNt = new Date;
    ckPageUser = ckPageNt.getTime() + '' + getRandom(100000000, 999999999);
    setCkCookie('_ck_uName', ckPageUser);
}

// chuanke
var ckPageUrl = '<img style="display:none" src="http://web.stat.chuanke.com/ckpage.php?h=';
ckPageUrl += encodeURIComponent(location.href) + '&r=' + encodeURIComponent(window.document.referrer);
ckPageUrl += '&ua=' + encodeURIComponent(navigator.userAgent) + '&s=' + screen.width + 'x' + screen.height;
ckPageUrl += '&c=' + screen.colorDepth + '&u=' + ckPageUser + '&rd=' + Math.floor(Math.random() * 999999999 + 1);
ckPageUrl += '" width="0" height="0">';
document.write(ckPageUrl);

// getID
var ckUserID = ckPageUser;
var authInfo = readCkCookie('AuthInfo');
if (typeof authInfo === 'string') {
    try {
        var userInfo = eval('(' + ckBase64Decode(authInfo) + ')');
        if (userInfo.userid > 0) {
            ckUserID = userInfo.userid;
        }
    }catch (e) {}
}

// Baidu
var _hmt = _hmt || [];
_hmt.push(['_setAccount', '2be0d6083ea4207036d33a4d8be519db']);
_hmt.push(['_setUserId', ckUserID]);

function ckBase64Decode(input) {
    var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    var output = '';
    var chr1 = '';
    var chr2 = '';
    var chr3 = '';
    var enc1 = '';
    var enc2 = '';
    var enc3 = '';
    var enc4 = '';
    var i = 0;
    if (input.length % 4 !== 0) {
        return '';
    }
    var base64test = /[^A-Za-z0-9\+\/\=]/g;
    if (base64test.exec(input)) {
        return '';
    }
    do {
        enc1 = keyStr.indexOf(input.charAt(i++));
        enc2 = keyStr.indexOf(input.charAt(i++));
        enc3 = keyStr.indexOf(input.charAt(i++));
        enc4 = keyStr.indexOf(input.charAt(i++));
        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;
        output = output + String.fromCharCode(chr1);
        if (enc3 !== 64) {
            output += String.fromCharCode(chr2);
        }
        if (enc4 !== 64) {
            output += String.fromCharCode(chr3);
        }
        chr1 = chr2 = chr3 = '';
        enc1 = enc2 = enc3 = enc4 = '';
    } while (i < input.length);
    return output;
}
function readCkCookie(name) {
    var cookie = '';
    var search = name + '=';
    if (document.cookie.length > 0) {
        var offset = document.cookie.indexOf(search);
        if (offset !== -1) {
            offset += search.length;
            var end = document.cookie.indexOf(';', offset);
            if (end === -1) {
                end = document.cookie.length;
            }
            cookie = document.cookie.substring(offset, end);
        }
    }
    return decodeURIComponent(cookie);
}
function getRandom(min, max) {
    return Math.floor(min + Math.random() * (max - min));
}
function setCkCookie(name, val) {
    var expires = new Date(2037, 11, 31); // month:0~11
    document.cookie = name + '=' + escape(val) + '; expires=' + expires.toGMTString() + ';path=/;domain=chuanke.com';
}
function getCkCookie(name) {
    var ck = document.cookie.split('; ');
    for (var i = 0; i < ck.length; i++) {
        var c = ck[i].split('=');
        if (name === c[0]) {
            return unescape(c[1]);
        }
    }
}